package com.honey.mybatis.generator;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.JavaVisibility;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;

public abstract class AbstractMethod extends AbstractMybatis{
	
	/** 空字符串数组 */
	protected static final String[] EMPTY_STRING_ARRAY =new String[0];
	 
	/** 空参数数组 */
	protected static final Parameter[] EMPTY_PARAMETER_ARRAY = new Parameter[0];
	
	/** 空异常数组 */
	protected static final FullyQualifiedJavaType[] EMPTY_EXCEPTION_ARRAY= new FullyQualifiedJavaType[0];
	
	/** 数据库结构 */
	protected IntrospectSchema introspectSchema ;
	
	/** 数据库结构包含的列 */
	protected IntrospectSchemaColumn introspectSchemaColumn;
	
	/** 方法所属的类 */
	protected AbstractJavaCompilation clazz;

	/**
	 * 慎用默认的构造函数
	 */
	protected AbstractMethod(){
		
	}
	
	/**
	 * 构造
	 * @param clazz
	 * @param belongPlugin
	 * @param introspectSchema
	 * @param introspectSchemaColumn
	 */
	protected AbstractMethod(
			AbstractJavaCompilation clazz,
			MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn
			) {
		super(belongPlugin);
		this.clazz = clazz;
		this.introspectSchema = introspectSchema;
		this.introspectSchemaColumn = introspectSchemaColumn;
	}
	/**
	 * 根据子类覆盖的方法创建新方法
	 * @return
	 */
	public final Method createMethod( ){
		Method answer = null;
		String methodName = getMethodName();
		if( methodName!=null ){
			answer = new Method(methodName,getReturnType(),getMethodDecoration());
			answer.addAnnotation(getAnnotation());
			answer.addParameter(getParameter());
			answer.addException(getException());
			answer.addJavaDocLine(getJavaDoc());
			answer.addBodyLine(getBody());
			answer.setBaseType(isBaseType());
			addBody(answer);
		}
		return answer;
	}
	
	/**
	 * 由子类返回方法名称
	 * @return
	 */
	protected abstract String getMethodName();
	
	/**
	 * 方法体内容.先执行本方法然后执行addBody方法,注意和addBody方法区别.
	 * @return
	 */
	protected String[] getBody(){
		return EMPTY_STRING_ARRAY;
	};
	
	/**
	 * 由子类添加方法体的内容.先执行getBody方法然后执行本方法方法,注意和getBody方法区别.
	 * @param method
	 * @return
	 */
	protected void addBody(Method  method){
		//return method;
	};
	
	/**
	 * 方法的返回类型,默认为空.
	 * @return
	 */
	protected FullyQualifiedJavaType getReturnType(){
		return null;
	};
	
	/**
	 * 方法抛出异常类型,默认为空.
	 * @return
	 */
	protected FullyQualifiedJavaType[] getException(){
		return EMPTY_EXCEPTION_ARRAY;
	};
	
	/**
	 * 方法修饰,默认使用public修饰.
	 * @return
	 */
	protected MethodDecoration getMethodDecoration(){
		return new MethodDecoration(JavaVisibility.PUBLIC);
	}
	
	/**
	 * 方法传入参数,默认为空.
	 * @return
	 */
	protected Parameter[] getParameter(){
		return EMPTY_PARAMETER_ARRAY;
	}
	
	/**
	 * 方法注释,默认为空.
	 * @return
	 */
	protected String[] getJavaDoc(){
		return EMPTY_STRING_ARRAY;
	};
	
	/**
	 * 方法注解,默认为空.
	 * @return
	 */
	protected String[] getAnnotation(){
		return EMPTY_STRING_ARRAY;
	}
	
	/**
	 * 是否使用基础类型
	 * @return
	 */
	protected boolean isBaseType(){
		return true;
	}
}
